package com.atguigu.flink.sql;

import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * Created by Smexy on 2022/12/21
 *
 *       tableEnv.sqlQuery( select sql)
 *
 *       tableEnv.executeSql(insert sql)
 */
public class Demo1_SQL
{
    public static void main(String[] args) {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        SingleOutputStreamOperator<WaterSensor> ds = env
            .socketTextStream("hadoop103", 8888)
            .map(new WaterSensorMapFunction());


        Table table = tableEnv.fromDataStream(ds);

        //执行查询
      /*  tableEnv.sqlQuery("select * from " + table + " where id = 's1' ")
                .execute()
                .print();*/


        //为动态表起个名字
        tableEnv.createTemporaryView("ws",table);

        tableEnv.sqlQuery("select * from ws where id = 's1' ")
                .execute()
                .print();




    }
}
